package work.zhengyun.demo.controller;

import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import work.zhengyun.demo.service.UserService;
import work.zhengyun.demo.vo.UserVo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhengyun
 * @date 2019/11/26 16:54
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/hi")
    public String hi(){
        return "hi shiro";
    }

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView();
        // 设置跳转的视图 默认映射到 src/main/resources/templates/{viewName}.html
        view.setViewName("index");
        // 设置属性
        view.addObject("title", "我的templates页面");
        view.addObject("desc", "欢迎进入我的csdn博客");
        Author author = new Author();
        author.setAge(18);
        author.setEmail("574860631@qq.com");
        author.setName("zhengyun");
        view.addObject("author", author);
        return view;
    }

    @RequestMapping("/index1")
    public String index1(HttpServletRequest request) {
        // TODO 与上面的写法不同，但是结果一致。
        // 设置属性
        request.setAttribute("title", "我的templates页面");
        request.setAttribute("desc", "欢迎进入我的跳转后的页面");
        Author author = new Author();
        author.setAge(18);
        author.setEmail("574860631@qq.com");
        author.setName("zhengyun");
        request.setAttribute("author", author);
        // 返回的 index 默认映射到 src/main/resources/templates/xxxx.html
        return "index1";
    }

    @RequestMapping("/userList")
    public String userList(HttpServletRequest request) {
        List<UserVo> userList = userService.findUsers();
        request.setAttribute("userList",userList);
        return "user/list";
    }
    @RequestMapping("/productList")
    public String productList() {
        return "product/list";
    }

    /*@RequestMapping("/add")
    public String add() {
        return "user/add";
    }

    @RequestMapping("/update")
    public String update() {
        return "user/update";
    }

    @RequestMapping("/setUser")
    public String setUser() {
        return "user/setUser";
    }*/

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/unAuth")
    public String unAuth() {
        return "unAuth";
    }

    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        System.out.println("===进入  login");
        /**
         * 使用shiro编写认证操作
         */
        //获取Subject
        Subject subject= SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        //执行登录方法
        try {
            //只要执行login方法，就会去执行UserRealm中的认证逻辑
            subject.login(token);

            //如果没有异常，代表登录成功
            //跳转到textThymeleaf页面，代表主页
            return "redirect:/index";
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            //登录失败
            model.addAttribute("msg","用户名不存在");
            return "login";

        }catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }

    @Data
    class Author {
        private int age;
        private String name;
        private String email;
    }
}
